using System;
using System.IO;
using ArticleManagementSystem.Api.Entity;
using ArticleManagementSystem.Api.Repostisory;
using Microsoft.AspNetCore.Hosting;

namespace ArticleManagementSystem.Api.Utils
{
    public class FilesHelper
    {

        public static dynamic UpImgs(dynamic files, IWebHostEnvironment env, IRepostisory<Imgs> imgsRespostisory)
        {
            var _imgsRespostisory = imgsRespostisory;
            var imgsRootPath = env.ContentRootPath;
            string filesPath = "";
            // var files = Request.Form.Files;
            string tempPath = DateTime.Now.ToString("yyyyMMdd");//相对路径
            string physicPath = imgsRootPath + "\\files\\" + tempPath;//物理路径

            if (!Directory.Exists(physicPath))
            {
                Directory.CreateDirectory(physicPath);
            }

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    //文件名字
                    string filesName = DateTime.Now.ToFileTime() + Path.GetExtension(formFile.FileName);
                    //绝对路径
                    string fileFullPath = Path.Combine(physicPath, filesName);
                    using (var stream = new FileStream(fileFullPath, FileMode.CreateNew))
                    {
                        formFile.CopyTo(stream);//保存文件
                    }
                    filesPath += fileFullPath + ',';

                }
            }

            filesPath = filesPath.TrimEnd(',');
            return filesPath;

        }
    }
}